﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.ServiceImp
{
    public class WCFPublishServiceHost : IWCFServiceHost
    {
        private ServiceHost _serviceHost;

        public bool Open()
        {
            Logger.InfoToTag("Server", "发布服务启动中...");
            Configuration.Server.ServiceConfig serviceConfig = Configuration.Server.Config.Host.GetServiceConfig("PublishService");
            Uri baseAddress = new Uri(string.Format("net.tcp://{0}:{1}/{2}/", serviceConfig.IP, serviceConfig.Port, serviceConfig.Path));

            _serviceHost = new ServiceHost(typeof(PublishService), baseAddress);
            Binding binding = new NetTcpBinding();
            (binding as NetTcpBinding).ReceiveTimeout = TimeSpan.FromSeconds(300);
            (binding as NetTcpBinding).SendTimeout = TimeSpan.FromSeconds(300);
            (binding as NetTcpBinding).Security.Mode = SecurityMode.None;
            (binding as NetTcpBinding).TransferMode = TransferMode.Streamed;
            (binding as NetTcpBinding).MaxBufferSize = 1024 * 1024 * 1024;
            (binding as NetTcpBinding).MaxReceivedMessageSize = 1024 * 1024 * 1024;
            ServiceMetadataBehavior serviceMetadataBehavior = new ServiceMetadataBehavior();
            _serviceHost.Description.Behaviors.Add(serviceMetadataBehavior);

            ServiceThrottlingBehavior serviceThrottlingBehavior = new ServiceThrottlingBehavior();
            serviceThrottlingBehavior.MaxConcurrentSessions = 10000;
            serviceThrottlingBehavior.MaxConcurrentCalls = 10000;
            serviceThrottlingBehavior.MaxConcurrentInstances = 20000;
            _serviceHost.Description.Behaviors.Add(serviceThrottlingBehavior);

            if (serviceConfig.MetadataGet)
            {
                Logger.InfoToTag("Server", "发布服务元数据获取开关:开启");
                Logger.InfoToTag("Server", "发布服务元数据获取地址:{0}mex", baseAddress);
                _serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
            }
            else
            {
                Logger.InfoToTag("Server", "发布服务元数据获取开关:关闭");
            }

            _serviceHost.AddServiceEndpoint(typeof(IPublishService), binding, baseAddress);
            Logger.InfoToTag("Server", "发布服务地址:{0}", baseAddress);

            try
            {
                _serviceHost.Open();
                Logger.InfoToTag("Server", "发布服务启动成功");
                return true;
            }
            catch (Exception ex)
            {
                Logger.ErrorToTag("Server", string.Format("发布服务启动失败{0}{1}", Environment.NewLine, ex));
                return false;
            }
        }

        public void Close()
        {
            Logger.InfoToTag("Server", "发布服务器关闭中...");
            try
            {
                _serviceHost.Close();
                Logger.InfoToTag("Server", "发布服务关闭成功");
            }
            catch (Exception ex)
            {
                Logger.ErrorToTag("Server", string.Format("发布服务关闭失败{0}{1}", Environment.NewLine, ex));
            }
        }
    }
}
